package com.mathdroid.core;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Properties;

import com.mathdroid.exceptionhandling.ExceptionHandler;

/**
 * This utility class gives easy access to the general properties file.
 */
public final class Config {

	/**
	 * Classpath to the properties file.
	 */
	private static final String CFG_PATH =
		"/com/mathdroid/core/cfg.properties";

	/**
	 * The properties container.
	 */
	private static Properties properties = null;

	/**
	 * Hidden standard constructor.
	 */
	private Config() { }

	/**
	 * Fill the properties object with the properties from the file.
	 */
	private static void init() {
		BufferedInputStream stream = null;
		stream = new BufferedInputStream(
				Config.class.getResourceAsStream(Config.CFG_PATH));
		try {
			Config.properties.load(stream);
		} catch (IOException e) {
			ExceptionHandler.report(
					"An error occured while reading from the input stream.", e);
		}
		try {
			stream.close();
		} catch (IOException e) {
			ExceptionHandler.report(
					"I/O error while closing the input stream.", e);
		}
	}

	/**
	 * Get a property. Properties are loaded on first use.
	 * @param s Name of the property.
	 * @return Value of the property.
	 */
	public static String getProperty(final String s) {
		if (Config.properties == null) {
			Config.properties = new Properties();
			Config.init();
		}
		return Config.properties.getProperty(s);
	}
}
